package org.example;

import jetbrains.datalore.plot.PlotSvgExport;
import org.jetbrains.letsPlot.intern.Plot;
import org.jetbrains.letsPlot.intern.ToSpecConvertersKt;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {
        exportUsingJava();
    }

    private static void exportUsingJava() {
        Plot plot = DemoUtil.createPlot();
        DemoUtil.save(plot);
    }

    private static void exportUsingKotlin() {
        DemoUtilKt instance = DemoUtilKt.INSTANCE;
        Plot plot = instance.createPlot();
        instance.save(plot);
    }

    /**
     * java 版本 demo
     * @throws IOException
     */
    private static void invokeJavaDemo() throws IOException {
        Plot plot = DemoUtil.createPlot();
        Map<String, Object> stringObjectMap = ToSpecConvertersKt.toSpec(plot);

        PlotSvgExport plotSvgExport = PlotSvgExport.INSTANCE;
        String content = plotSvgExport.buildSvgImageFromRawSpecs(stringObjectMap, null);

        DemoUtil.openInBrowser(content);
    }

    /**
     * kotlin 版本 demo
     */
    private static void invokeKotlinDemo() {
        DemoUtilKt instance = DemoUtilKt.INSTANCE;

        Random random = new Random();
        Map<String, List<Double>> data = new HashMap<>();

        List<Double> points = Stream.generate(random::nextGaussian).limit(4000).collect(Collectors.toList());
        data.put("x", points);

        Plot plot = instance.createPlot(data);
        instance.openInBrowser(plot);
    }
}
